<?php
  $baseUrl = Yii::app()->baseUrl; 
  $cs=Yii::app()->getClientScript();
  $cs->registerScriptFile('http://maps.google.com/maps/api/js?sensor=false', CClientScript::POS_HEAD);
  $cs->registerScriptFile($baseUrl.'/scripts/google_map_handler.js', CClientScript::POS_END);
  $cs->registerScriptFile($baseUrl.'/scripts/account_type_handler.js', CClientScript::POS_END);
?>

<?php $this->pageTitle=Yii::app()->name . ' - '.UserModule::t("Profile");
$this->breadcrumbs=array(
  UserModule::t("Profile")=>array('profile'),
  UserModule::t("Edit"),
);
?><h2><?php echo UserModule::t('Edit profile'); ?></h2>
<?php echo $this->renderPartial('menu'); ?>

<?php if(Yii::app()->user->hasFlash('profileMessage')): ?>
<div class="success">
<?php echo Yii::app()->user->getFlash('profileMessage'); ?>
</div>
<?php endif; ?>
<div class="form">
<?php $form=$this->beginWidget('UActiveForm', array(
  'id'=>'profile-form',
  'enableAjaxValidation'=>true,
  'htmlOptions' => array('enctype'=>'multipart/form-data'),
)); ?>

  <p class="note"><?php echo UserModule::t('Fields with <span class="required">*</span> are required.'); ?></p>

  <?php echo $form->errorSummary(array($model,$profile)); ?>
  
<?php 
    $profileFields=$profile->getFields();
    if ($profileFields) {
      foreach($profileFields as $field) {
      ?>
  <?php 
    $row_class = 'row';
    $row_class .= $profile->try_get_class_for_hidden_field($field);
    $row_class .= $profile->try_get_class_for_company_field($field);
    $row_class .= $profile->try_get_class_for_only_private_field($field);
  ?>
  <div id="<?php echo $field->varname; ?>" class="<?php echo $row_class; ?>">
    <?php echo $form->labelEx($profile,$field->varname);
    
    if ($field->widgetEdit($profile)) {
      echo $field->widgetEdit($profile);
    } elseif ($field->range) {
      echo $form->dropDownList($profile,$field->varname,Profile::range($field->range));
    } elseif ($field->field_type=="TEXT") {
      echo $form->textArea($profile,$field->varname,array('rows'=>6, 'cols'=>50));
    } elseif ($field->field_type=="HIDDEN_VARCHAR") {  
      echo $form->textField($profile,$field->varname,array('size'=>60,'maxlength'=>(($field->field_size)?$field->field_size:255)));
    } else {
      echo $form->textField($profile,$field->varname,array('size'=>60,'maxlength'=>(($field->field_size)?$field->field_size:255)));
    }
    echo $form->error($profile,$field->varname); ?>
  </div>	
      <?php
      }
    }
?>
    
  <div class="row">
    <input type="checkbox" id="Profile_show_me_on_map" name="Profile[show_me_on_map]"/><label for="Profile_show_me_on_map" class="inline">Pokazuj moją lokalizację na mapie</label>
  </div>
  
  <?php // google maps ?>
  <div class="map_section">
    <div id="agent_map"><!-- . --></div>
  </div>
  <?php // end google maps ?>

  <div class="row">
    <?php echo $form->labelEx($model,'username'); ?>
    <?php echo $form->textField($model,'username',array('size'=>20,'maxlength'=>20)); ?>
    <?php echo $form->error($model,'username'); ?>
  </div>

  <div class="row">
    <?php echo $form->labelEx($model,'email'); ?>
    <?php echo $form->textField($model,'email',array('size'=>60,'maxlength'=>128)); ?>
    <?php echo $form->error($model,'email'); ?>
  </div>
  
  <div class="row buttons">
    <?php echo CHtml::submitButton($model->isNewRecord ? UserModule::t('Create') : UserModule::t('Save')); ?>
  </div>

<?php $this->endWidget(); ?>

</div><!-- form -->

<script type='text/javascript'>
  jQuery(document).ready(function(jQuery){
    initialize_map(document.getElementById("agent_map"));
    var form_fields_data = <?php echo CJavaScript::encode($profile->get_fields_js_data());?>;
    var account_type = <?php echo $profile->get_account_type();?>;
    rearrange_fields_by_type(form_fields_data, account_type);
  });
</script>